package com.yanqu.road.dao.impl.activity.hanghai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hanghai.HangHaiGoods;
import com.yanqu.road.entity.activity.hanghai.HangHaiPort;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class HangHaiPortDaoImpl extends TempDataObjectDao<HangHaiPort> {

    public static String getHangHaiPortMapSql = "select * from t_c_activity_hanghai_port where activityId=?";

    public Map<String, Map<Integer, HangHaiPort>> getHangHaiPortMap(int activityId) {
        Map<String, Map<Integer, HangHaiPort>> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getHangHaiPortMapSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HangHaiPort hangHaiPort = parse(rs);

                if (!map.containsKey(hangHaiPort.getUnionUid())) {
                    map.put(hangHaiPort.getUnionUid(), new ConcurrentHashMap<>());
                }

                map.get(hangHaiPort.getUnionUid()).put(hangHaiPort.getPortId(), hangHaiPort);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateHangHaiPortSql = "update t_c_activity_hanghai_port set specId=?,lackId=?,goodsMap=?,reportUserId=? where activityId=? and unionUid=? and portId=?";

    @Override
    protected boolean updateData(HangHaiPort hangHaiPort) {
        hangHaiPort.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getSpecId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getLackId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiPort.getGoodsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiPort.getReportUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiPort.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getPortId()));
        if (getDb().executeNoneQuery(updateHangHaiPortSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertHangHaiPortSql = "insert into t_c_activity_hanghai_port(activityId,unionUid,portId,specId,lackId,goodsMap,reportUserId) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(HangHaiPort hangHaiPort) {
        hangHaiPort.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, hangHaiPort.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getPortId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getSpecId()));
        para.put(i++, new DbParameter(Types.INTEGER, hangHaiPort.getLackId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(hangHaiPort.getGoodsMap())));
        para.put(i++, new DbParameter(Types.BIGINT, hangHaiPort.getReportUserId()));
        if (getDb().executeNoneQuery(insertHangHaiPortSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected HangHaiPort parseData(ResultSet rs) throws SQLException {
        HangHaiPort hangHaiPort = new HangHaiPort();
        hangHaiPort.setActivityId(rs.getInt("activityId"));
        hangHaiPort.setUnionUid(rs.getString("unionUid"));
        hangHaiPort.setPortId(rs.getInt("portId"));
        hangHaiPort.setSpecId(rs.getInt("specId"));
        hangHaiPort.setLackId(rs.getInt("lackId"));
        hangHaiPort.setGoodsMap(JSONObject.parseObject(rs.getString("goodsMap"), new TypeReference<Map<Integer, HangHaiGoods>>() {
        }));
        hangHaiPort.setReportUserId(rs.getLong("reportUserId"));
        return hangHaiPort;
    }

}
